package com.cxx.purchasecharge.component.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cxx.purchasecharge.component.bean.ActionResult;
import com.cxx.purchasecharge.component.bean.BaseBean;
import com.cxx.purchasecharge.component.bean.DataGridRequestForm;
import com.cxx.purchasecharge.component.bean.SearchRequestForm;
import com.cxx.purchasecharge.component.utils.AjaxUtils;

@Controller
public abstract class GenericController <B extends BaseBean>
{
    @SuppressWarnings ("unused")
    private static final Logger logger = Logger.getLogger (GenericController.class);

    private String viewName;
    
    public static final String REQUESTMAPPING_CHECK_EXIST = "/checkExist";
    public static final String REQUESTMAPPING_GET_ALL_MODEL = "/getAllModel";
    public static final String REQUESTMAPPING_GET_MODEL_BY_SEARCH_FORM = "/getModelBySearchForm";
    public static final String REQUESTMAPPING_GET_MODEL_BY_ID = "/getModelById";
    public static final String REQUESTMAPPING_GET_MODEL_BY_ID_WITH_RESPONSE = "/getModelByIdWithResponse";
    public static final String REQUESTMAPPING_ADD_MODEL = "/addModel";
    public static final String REQUESTMAPPING_ADD_MODEL_WITH_RESPONSE = "/addModelWithResponse";
    public static final String REQUESTMAPPING_UPDATE_MODEL = "/updateModel";
    public static final String REQUESTMAPPING_UPDATE_MODEL_WITH_RESPONSE = "/updateModelWithResponse";
    public static final String REQUESTMAPPING_DELETE_MODEL = "/deleteModel";
    public static final String REQUESTMAPPING_DELETE_MODELS = "/deleteModels";

    @RequestMapping (value = "/")
    public String initView (ModelMap model)
    {
        return this.viewName;
    }

    @RequestMapping (value = REQUESTMAPPING_CHECK_EXIST, method = RequestMethod.POST)
    public @ResponseBody
    String checkExist (@RequestParam String id, @RequestParam String value, HttpServletRequest request)
    {
        if (StringUtils.isBlank (id))
        {
            // new
        }
        else
        {
            // edit
        }

        return "true";
    }
    
    @RequestMapping (value = REQUESTMAPPING_GET_ALL_MODEL, method = RequestMethod.POST)
    public @ResponseBody
    String getAllModel (DataGridRequestForm dataGridRequestForm, HttpServletRequest request)
    {
        return "";
    }
    
    @RequestMapping (value = REQUESTMAPPING_GET_MODEL_BY_SEARCH_FORM, method = RequestMethod.POST)
    public @ResponseBody
    String getModelBySearchForm (DataGridRequestForm dataGridRequestForm, SearchRequestForm searchRequestForm, HttpServletRequest request)
    {
        return "";
    }
    
    @RequestMapping (value = REQUESTMAPPING_GET_MODEL_BY_ID, method = RequestMethod.POST)
    public @ResponseBody String getModelById (@RequestParam String id, HttpServletRequest request)
    {
        return "";
    }

    @RequestMapping (value = REQUESTMAPPING_GET_MODEL_BY_ID_WITH_RESPONSE, method = RequestMethod.POST)
    public void getModelByIdWithResponse (@RequestParam String id, HttpServletResponse response) throws Exception
    {
        throw new Exception("Not implemented");
    }

    @RequestMapping (value = REQUESTMAPPING_ADD_MODEL, method = RequestMethod.POST)
    public @ResponseBody
    String addModel (B bean, BindingResult bindingResult, HttpServletRequest request)
    {
        return AjaxUtils.getJsonObject (ActionResult.createActionResult ().build ());
    }
    
    @RequestMapping (value = REQUESTMAPPING_ADD_MODEL_WITH_RESPONSE, method = RequestMethod.POST)
    public void addModelWithResponse (B bean, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response)
        throws Exception
    {
        throw new Exception("Not implemented");
    }
    
    @RequestMapping (value = REQUESTMAPPING_UPDATE_MODEL, method = RequestMethod.POST)
    public @ResponseBody
    String updateModel (B bean, BindingResult bindingResult, HttpServletRequest request)
    {
        return AjaxUtils.getJsonObject (ActionResult.createActionResult ().build ());
    }

    @RequestMapping (value = REQUESTMAPPING_UPDATE_MODEL_WITH_RESPONSE, method = RequestMethod.POST)
    public void updateModelWithResponse (B bean, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response)
        throws Exception
    {
        throw new Exception("Not implemented");
    }

    @RequestMapping (value = REQUESTMAPPING_DELETE_MODEL, method = RequestMethod.POST)
    public @ResponseBody
    String deleteModel (@RequestParam String id, HttpServletRequest request)
    {
        return AjaxUtils.getJsonObject (ActionResult.createActionResult ().build ());
    }

    @RequestMapping (value = REQUESTMAPPING_DELETE_MODELS, method = RequestMethod.POST)
    public @ResponseBody
    String deleteModels (@RequestParam String ids, HttpServletRequest request)
    {
        return AjaxUtils.getJsonObject (ActionResult.createActionResult ().build ());
    }

    public String getViewName()
    {
        return viewName;
    }

    public void setViewName(String viewName)
    {
        this.viewName = viewName;
    }
}
